package com.tao.design.proxy.demo2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @ClassName MyInvocationHandler
 * @Description 实现代理逻辑的类
 * @Author Hugo
 * @Date 2023/3/25 18:27
 * @Version 1.0
 **/
public class MyInvocationHandler implements InvocationHandler {
    private Object target;

    private Object[] args;

    public MyInvocationHandler(Object target,Object[] args) {
        this.target = target;
        this.args =args;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("Invoking sayHello");
        long start = System.currentTimeMillis();
        Object result = method.invoke(target, args);
        String name = method.getName();
        Thread.sleep(1000);
        long end = System.currentTimeMillis();
        if (result != null){
            System.out.println("返回返回结果:"+result);
        }
        if (name != null) {
            System.out.println("运行方法" + name + "消耗时间" + (end - start));
        }

        System.out.println("Invoking ending!");
        return result;
    }
}
